﻿using System;
using VIT.Web.UI;
using VIT.Web.Controls;
using VIT.Provider;
using VIT.Library.Web;
using System.Data;
using System.Collections.Generic;

namespace VIT.Web.Admin
{
    public partial class WebConfig : VITComponent
    {
        VIT.Provider.WebConfig obj;

        protected void Page_Load(object sender, EventArgs e)
        {
            
            obj = new VIT.Provider.WebConfig();

            obj.LanguageId = Session["language"].ToString();

            if (!IsPostBack)
            {
                loadLanguages();
                loadPreTemplate();
                loadAdminTemplate();
                loadData();
            }
        }

        private void loadData()
        {
            obj.LanguageId = ddlLang.Text;
            obj.GetSingle();
            ddlTemplate.Text = obj.PreTemplate;
            ddlAdminTemplate.Text = obj.AdminTemplate;            
        }

        private void loadLanguages()
        {
            Language language = new Language();
            
            ddlLang.DataSource = language.getAll();
            ddlLang.DataTextField = "Name";
            ddlLang.DataValueField = "Id";
            ddlLang.DataBind();

            ddlLang.SelectedValue = Session["language"].ToString();
        }

        private void loadPreTemplate()
        {
            string TemplatePath = URLBaseDir + "Templates";
            ddlTemplate.DataSource = MainCore.ShowDirectories(TemplatePath);
            ddlTemplate.DataBind();
        }
        private void loadAdminTemplate()
        {
            string TemplatePath = URLBaseDir + "Admin/Templates";
            ddlAdminTemplate.DataSource = MainCore.ShowDirectories(TemplatePath);
            ddlAdminTemplate.DataBind();
        }

        private void save()
        {
            obj.LanguageId = ddlLang.SelectedValue;
            obj.PreTemplate = ddlTemplate.Text;
            obj.AdminTemplate = ddlAdminTemplate.Text;

            if (obj.UpdateObj() > 0) setTrueVisible(lblThanhCong);
            else setTrueVisible(lblThatBai);
        }

        private void setTrueVisible(System.Web.UI.HtmlControls.HtmlGenericControl control)
        {
            lblThanhCong.Visible = false;
            lblThatBai.Visible = false;

            control.Visible = true;
        }

        protected void Apply_Click(object sender, EventArgs e)
        {
            save();
            loadData();
        }
        protected void Save_Click(object sender, EventArgs e)
        {
            save();
            RedirectComponent("Home");
        }

        protected void ddlTemplate_SelectedIndexChanged(object sender, EventArgs e)
        {
            obj.LanguageId = ddlLang.SelectedItem.Text;
            loadData();
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            RedirectComponent("Home");
        }
    }
}
